/**
 * 自定义
 * */

var apiDomain = '/api/item/fxotary/admin';
var apis = {
    leftmenu: { //后台菜单
        method: 'post',
        url: apiDomain + '/leftmenu',
    },
    adminIndex: { //管理员
        method: 'post',
        url: apiDomain + '/admin/index',
    },
    adminOne: {
        method: 'post',
        url: apiDomain + '/admin/one',
    },
    adminModify: {
        method: 'post',
        url: apiDomain + '/admin/modify',
    },
    adminPwd: {
        method: 'post',
        url: apiDomain + '/admin/pwd',
    },
    actionIndex: { //后台功能
        method: 'post',
        url: apiDomain + '/action/index',
    },
    actionOne: {
        method: 'post',
        url: apiDomain + '/action/one',
    },
    actionModify: {
        method: 'post',
        url: apiDomain + '/action/modify',
    },
    yewuIndex: { //业务
        method: 'post',
        url: apiDomain + '/yewu/index',
    },
    yewuOne: {
        method: 'post',
        url: apiDomain + '/yewu/one',
    },
    yewuModify: {
        method: 'post',
        url: apiDomain + '/yewu/modify',
    },
};


(function () {
    if (window.location.pathname != '/admin/login.html') {
        header();
        leftMenu();
    }
})();


//顶部header
function header(){
    //var adminInfo = getAdminInfo();
    var h = '';
    h += '<style type="text/css"> #apiDoc { color: #fff; cursor: pointer; position: absolute; top: 10px; left: 200px; } #apiDoc:hover { color: #0088cc; } </style>';
    h += '<div class="navbar navbar-fixed-top">';
    h += '<div class="container-fluid cl">';
    h += '<a class="logo navbar-logo f-l mr-10 hidden-xs" href="index.html">';
    h += 'fxotary后台管理';
    h += '</a>';
    //h += '<a href="https://www.eolink.com/share/index?shareCode=a909486f822c1c094b178cf67a7b360c" target="_blank" id="apiDoc">接口文档</a>';
    h += '<nav id="Hui-userbar" class="nav navbar-nav navbar-userbar hidden-xs">';
    h += '<ul class="cl">';
    h += '<li>系统管理员</li>';
    h += '<li class="dropDown dropDown_hover">';
    h += '<a href="javascript:;" class="dropDown_A">';
    h += '<span>admin</span> ';
    h += '<i class="Hui-iconfont">&#xe6d5;</i>';
    h += '</a>';
    h += '<ul class="dropDown-menu menu radius box-shadow">';
    h += '<li><a href="javascript:;" onclick="">登录信息</a></li>';
    h += '<li><a href="javascript:;" onclick="logout();">退出</a></li>';
    h += '</ul>';
    h += '</li>';
    h += '</ul>';
    h += '</nav>';
    h += '</div>';
    h += '</div>';
    h += '';
    $('.navbar-wrapper').html(h);
}


//左侧菜单
function leftMenu(){
    //var adminInfo = getAdminInfo();
    //console.log(adminInfo);
    //if (!adminInfo || !adminInfo.token) {
    //    layerMsgErr('没有登录');
    //    setTimeout(function(){
    //        window.location.href = 'login.html';
    //    }, 1000);
    //    return
    //}
    $.ajax({
        type: apis.leftmenu.method,
        url: apis.leftmenu.url,
        data: { token: '2b47d0965162d6e4071a2ccea919255f' },
        success: function (res) {
            //console.log(res);
            //if (res.code == -3) {
            //    msgPop({msg: '登录已过期'});
            //    setTimeout(function(){
            //        window.location.href = 'login.html';
            //    }, 1000);
            //    return
            //}
            if (res.code < 0) {
                console.log(res); return
            }
            leftMenuHtml(res.data);
        },
        error: function () {
            console.log('错误了！');
        }
    });
}


////左侧菜单
//function leftMenu(){
//    var prefix = 'file:///D:/phpstudy_pro/WWW/gsai/api/public/ai_items';
//    var ms = [
//        { id: 1, title: '首页', icon: '&#xe61a;', link: prefix + '/admin/index.html', childList: [] },
//        { id: 2, title: '后台管理', icon: '&#xe62d;', link: '', childList: [
//            { id: 201, title: '管理员', link: prefix + '/admin/admin/admin.html' },
//            { id: 202, title: '后台功能', link: prefix + '/admin/admin/menu.html' },
//            { id: 203, title: '前台页面', link: prefix + '/admin/admin/front.html' },
//        ] },
//        { id: 3, title: '前台管理', icon: '&#xe620;', link: '', childList: [
//            { id: 301, title: 'HOME1', link: prefix + '/172602699766e114f5200e1/index.html' },
//            { id: 302, title: 'HOME2', link: prefix + '/172602699766e114f5200e1/index_2.html' },
//            { id: 303, title: 'ABOUT', link: prefix + '/172602699766e114f5200e1/about_us.html' },
//            { id: 304, title: 'SERVICE LIST', link: prefix + '/172602699766e114f5200e1/service.html' },
//            { id: 305, title: 'SERVICE DETAIL', link: prefix + '/172602699766e114f5200e1/service_details.html' },
//        ] },
//    ];
//    leftMenuHtml(ms);
//}


//菜单字符串
function leftMenuHtml(ms){
    //var preUrl = window.location.pathname; //当前相对路径
    var prefix = '/ai_items/temp/fxotary';
    console.log(ms);
    var h = '', i, childList;
    h += '<style type="text/css">.mlyjt { ;margin-right: 10px; float: right; } .mla { background: #ffffff; }</style>';
    for (i = 0; i < ms.length; i ++) {
        childList = ms[i].childList;
        h += '<dl >';
        if (childList.length > 0) {
            h += '<dt onclick="lmQiehuan(' + ms[i].id + ');">';
            h += '<i class="Hui-iconfont">' + ms[i].icon + '</i>';
            h += ' &nbsp;' + ms[i].title;
            h += '<i class="Hui-iconfont menu_dropdown-arrow">&#xe6d5;</i>';
            h += '</dt>';
            h += '</dt>';
        } else {
            h += '<a href="' + prefix + ms[i].link + '">';
            h += '<dt>';
            h += '<i class="Hui-iconfont">&#xe620;</i>';
            h += ' &nbsp;' + ms[i].title;
            h += '</dt>';
            h += '</a>';
        }
        h += '<dd style="display: block;" id="lmChild' + ms[i].id + '">';
        if (childList.length > 0) {
            h += '<ul>';
            for (var ii = 0; ii < childList.length; ii ++) {
                h += '<li>';
                if (childList[ii].pid == 3) {
                    h += '<a href="' + prefix + childList[ii].link + '?menu_id=' + childList[ii].id + '&menu_title=' + encodeURI(childList[ii].title) + '">';
                } else {
                    h += '<a href="' + prefix + childList[ii].link + '">';
                }
                h += childList[ii].title;
                h += '</a>';
                h += '</li>';
            }
            h += '</ul>';
        }
        h += '</dd>';
        h += '</dt>';
        h += '';
        h += '</dl>';
    }
    $('#leftMenu').html(h);
}


//左侧切换菜单
function lmQiehuan(v){
    $('#lmChild' + v).toggle(500);
}


//分页字符串
function pageHtml(e){
    console.log(e);
    var start = e.start, limit = e.limit, total = e.total, url = e.url;
    var pageTotal = Math.ceil(total / limit);
    var h = '';
    h += '<style type="text/css"> #pageHtml {margin: 10px 0; width: 100%; text-align: center; color: #555; } #pageBtn {  color:#555; } #pageBtn a { padding: 5px 10px; color: #0088cc; } </style>';
    h += '显示 <span id="pageCurr">' + start + '</span> / <span id="pageTotal">' + pageTotal + '</span> 页，';
    h += '每页显示';
    h += '<select style="margin: 0 5px; padding: 2px 5px; border: 1px solid #eee;" onchange="listBySel(this.value);">';
    h += '<option value="5"';
    h += pages.limit==5 ? ' selected' : '';
    h += '>5</option>';
    h += '<option value="10"';
    h += pages.limit==10 ? ' selected' : '';
    h += '>10</option>';
    h += '<option value="20"';
    h += pages.limit==20 ? ' selected' : '';
    h += '>20</option>';
    h += '</select>';
    h += '<span id="pageBtn">';
    h += '<a href="javascript:;" onclick="pageHome();">首页</a>';
    h += '<a href="javascript:;" onclick="pagePre();">上一页</a>';
    //h += '<a href="javascript:;" onclick="msgPop({msg: \'当前第' + start + '页\'});">' + start + '</a>';
    h += '<a href="javascript:;" onclick="pageNext();">下一页</a>';
    h += '<a href="javascript:;" onclick="pageEnd();">尾页</a>';
    h += '</span>';
    $('#pageHtml').html(h);
    $('#pageTotal').html(total);
}
function listBySel(v){
    pages.limit = v;
    if (pages.url == '/admin/uctmp.html') {
        getTmpList();
    } else {
        getList();
    }
}
//首页
function pageHome(){
    window.location.href = pages.url;
}
//上一页
function pagePre(){
    var start = pages.start * 1 - 1;
    if (start <= 0) {
        msgPop({msg: '已经是第一页了'});
        return
    }
    window.location.href = pages.url + '?start=' + start;
}
//下一页
function pageNext(){
    var start = pages.start * 1 + 1;
    var pageTotal = Math.ceil(pages.total / pages.limit);
    //console.log(start, pageTotal);
    if (start > pageTotal) {
        msgPop({msg: '已经是最后一页了'});
        return
    }
    window.location.href = pages.url + '?start=' + start;
}
//尾页
function pageEnd(){
    var pageTotal = Math.ceil(pages.total / pages.limit);
    window.location.href = pages.url + '?start=' + pageTotal;
}


//抓图
function uploadImage() {
    var img = $("#viewimg"), dataURL, fileObj = $('#upload')[0],
        windowURL = window.URL || window.webkitURL, reader = new FileReader();
    dataURL = windowURL.createObjectURL(fileObj.files[0]); //要显示图片的路径
    img.attr("src", dataURL); //显示图片
    reader.readAsDataURL(fileObj.files[0]); //这一步是关键
    reader.onload = function (e){
        $('#img')[0].value = e.target.result; //这一步是关键
    }
}


//自定义弹窗
function msgPop(e){
    var cw = document.documentElement.clientWidth;
    var ch = document.documentElement.clientHeight;
    var msg = e.msg;
    var h = '';
    h += '<div style="position: fixed; top: 0; left: 0; z-index: 10000;">';
    h += '<div style="width: ' + (cw) + 'px; height: ' + (ch) + 'px; background: #000; opacity: 0.2; position: fixed; top: 0; left: 0; z-index: 10000;"></div>';
    h += '<div style="padding: 10px 20px; width: ' + (cw * 0.5) + 'px; text-align: center; background: #000; color: #FFF; border-radius: 5px; position: fixed; bottom: 40%; left: ' + ((cw * 0.5 - 40) / 2) + 'px; z-index: 10000;">'+msg+'</div>';
    h += '</div>';
    h += '<script>setTimeout(function(){$(\'#msgPop\').html(\'\');},2000)</script>';
    $('#msgPop').html(h);
}


//layer弹窗
function layerMsgErr(e){
    layer.msg(e,{icon: 0,time: 1000});
    return
}


//layer弹窗
function layerMsgOk(e){
    layer.msg(e,{icon: 1,time: 1000});
    return
}


//获取地址栏中参数
function getParamByUrl(name){
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r==null) return '';
    return  unescape(r[2]);
}


//显示前几个字符
function strPrefix(str, limit){
    if (str.length > limit) {
        return str.substring(0, limit) + '...';
    } else {
        return str;
    }
}


//显示后几个字符
function strSuffix(str, limit){
    //console.log(str.length, limit);
    if (str.length > limit) {
        return '...' + str.slice(- limit);
    } else {
        return str;
    }
}


//获取登录信息
function getAdminInfo(){
    return getCache(adminLogin);
}


//设置登录信息
function setAdminInfo(e){
    return setCache({key: adminLogin, val: e.val});
}


//退出登录
function logout(){
    console.log('退出登录');
    delCache({key: adminLogin});
    window.location.href = '/admin/login.html';
}


//设置缓存
function setCache(e){
    var key = e.key, val = JSON.stringify(e.val);
    window.localStorage.setItem(key, val);
}


//获取缓存
function getCache(key){
    var cache = window.localStorage.getItem(key);
    if (!cache) { return ''; }
    return JSON.parse(cache);
}


//删除指定缓存
function delCache(e){
    var key = e.key;
    window.localStorage.removeItem(key);
}


//清理缓存
function clearCache(){
    window.localStorage.clear();
}